package org.smartcti.freeswitch.outbound;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;

import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.IConnectHandler;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.IDisconnectHandler;
import org.xsocket.connection.INonBlockingConnection;

public class EchoHandler implements IDataHandler,IConnectHandler,IDisconnectHandler    {

	public boolean onData(INonBlockingConnection nbc) throws IOException,
			BufferUnderflowException, ClosedChannelException,
			MaxReadSizeExceededException {
		System.out.println("onData");
		String data = nbc.readStringByDelimiter("\n\n");
	    System.out.println("data is:\n"+data);
	    
	    nbc.write("sendmsg\ncall-command: execute\nexecute-app-name: answer\n\n");
	    nbc.flush();

		return true;
	}

	public boolean onConnect(INonBlockingConnection nbc) throws IOException,
			BufferUnderflowException, MaxReadSizeExceededException {
		System.out.println("onConnect");
		nbc.write("connect\n\n");
		nbc.flush();
		return true;
	}

	public boolean onDisconnect(INonBlockingConnection nbc) throws IOException {
		System.out.println("onDisconnect");
		String data = nbc.readStringByDelimiter("\n\n");
	    System.out.println("data is:\n"+data);
		return true;
	}

	
}
